package com.mt.vsphere.business.vsphere;

import com.vmware.connection.helpers.GetMOREF;
import com.vmware.vim25.*;
import lombok.extern.slf4j.Slf4j;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.SOAPFaultException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class VCenterConnectHelper {
    private  final ManagedObjectReference SVC_INST_REF = new ManagedObjectReference();
    public VimService vimService;
    public VimPortType vimPort;
    public GetMOREF getMOREFs;
    public ServiceContent serviceContent;
    private final String SVC_INST_NAME = "ServiceInstance";
    private Boolean isConnected = false;
    public  ManagedObjectReference perfManager;
    public  ManagedObjectReference propCollectorRef;

    public  void trustAllHttpsCertificates() throws Exception
    {
//        vimPort.findByUuid()
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
        javax.net.ssl.TrustManager tm = new TrustAllTrustManager();
        trustAllCerts[0] = tm;
        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
        javax.net.ssl.SSLSessionContext sslsc = sc.getServerSessionContext();
        sslsc.setSessionTimeout(0);
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }

    /**
     * @功能描述 连接认证
     */
    public  void connect(String url,String userName,String password)
    {
        try {
            HostnameVerifier hv = (urlHostName, session) -> true;
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            SVC_INST_REF.setType(SVC_INST_NAME);
            SVC_INST_REF.setValue(SVC_INST_NAME);
            vimService = new VimService();
            vimPort = vimService.getVimPort();
            Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();
            ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
            ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);

            serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF);
            vimPort.login(serviceContent.getSessionManager(), userName, password, null);
            isConnected = true;
            perfManager = serviceContent.getPerfManager();
            propCollectorRef = serviceContent.getPropertyCollector();

            log.info("connected server:{} serverName:{} serverType:{}",url,serviceContent.getAbout().getFullName(),serviceContent.getAbout().getApiType());
        } catch (Exception e) {
            log.info("connected server error :{} ",url);
            e.printStackTrace();
        }
    }

    /**
     * @功能描述 断开连接
     * @return
     * @throws Exception
     */
    public void disconnect() throws Exception
    {
        if (isConnected)
        {
            vimPort.logout(serviceContent.getSessionManager());
        }
        isConnected = false;
    }


}
